package team.bluepen.supermarket.data.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import team.bluepen.supermarket.constant.Constant;

import java.io.IOException;

/**
 * 创建Hbase连接
 * @author Kai
 */
public class HbaseConnectHelper {
    private HbaseConnectHelper() {}

    /**
     * 使用默认IP地址创建{@code Connection}
     * @return {@code Connection}
     */
    public static Connection create() throws IOException {
        return create(Constant.IP_ADDRESS);
    }

    /**
     * 使用指定IP地址创建{@code Connection}
     * @return {@code Connection}
     */
    public static Connection create(String address) throws IOException {
        return ConnectionFactory.createConnection(
                initConfiguration(address));
    }

    private static Configuration initConfiguration(String ip) {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", ip);
        return config;
    }
}
